package com.wwj.my_ummary.nodes.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wwj.my_ummary.nodes.entity.Nodes;
import com.wwj.my_ummary.nodes.mapper.NodesMapper;
import com.wwj.my_ummary.nodes.service.NodesService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wwj
 * @since 2021-02-05
 */
@Service
public class NodesServiceImpl extends ServiceImpl<NodesMapper, Nodes> implements NodesService {

    @Override
    public Object all() {

        QueryWrapper<Nodes> wrapper = new QueryWrapper<>();

        List<Nodes> nodes = baseMapper.selectList(wrapper);

        // 构建node列表
        List<TreeNode<Integer>> nodeList = CollUtil.newArrayList();

        for (Nodes node : nodes) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("url", node.getUrl());
            nodeList.add(new TreeNode<>(node.getId(), node.getParentid(), node.getName(), node.getWeight()).setExtra(map));
        }
        //构建树
        List<Tree<Integer>> treeList = TreeUtil.build(nodeList, 0);

        return treeList;
    }

    /**
     * 锁className+#+test1
     * key过期设置为100毫秒
     */
    @Override
    public void ssss(String userId) {
        System.out.println(userId);
    }
}
